<template>
  <div class="user-page">
    <div class="user">
      <img :src="user.avatar" alt="" />
      <h3>{{ user.username }}</h3>
    </div>
    <van-grid clickable :column-num="3" :border="false">
      <van-grid-item icon="clock-o" text="历史记录" to="/" />
      <van-grid-item icon="bookmark-o" text="我的收藏" to="/collect" />
      <van-grid-item icon="thumb-circle-o" text="我的点赞" to="/like" />
    </van-grid>

    <van-cell-group class="mt20">
      <van-cell title="推荐分享" is-link />
      <van-cell title="意见反馈" is-link />
      <van-cell title="关于我们" is-link />
      <van-cell title="退出登录" @click="lgOut" is-link />
    </van-cell-group>
  </div>
</template>

<script lang="ts" setup>
import type { UserInfo } from '@/types/data'
import { ref, onMounted } from 'vue'
import { getUserInfo } from '@/api/user'
import { Dialog } from 'vant'
import { logout } from '@/api/user'
import useTokenStore from '@/stores/token'
import { useRouter } from 'vue-router'
import 'vant/es/dialog/style'

const user = ref<Partial<UserInfo>>({})
const { clearToken } = useTokenStore()
const router = useRouter()

onMounted(() => {
  getUser()
})
const getUser = async () => {
  const { data } = await getUserInfo()
  user.value = data
}
const lgOut = () => {
  Dialog.confirm({
    title: '提示',
    message:
      '确认要退出登录吗',
  }).then(async () => {
    await logout()
    clearToken()
    router.push("/login") // 回登录页
  })
}
</script>

<style lang="less" scoped>
.user-page {
  padding: 0 10px;
  background: #f5f5f5;
  height: 100vh;

  .mt20 {
    margin-top: 20px;
  }

  .user {
    display: flex;
    padding: 20px 0;
    align-items: center;

    img {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      overflow: hidden;
    }

    h3 {
      margin: 0;
      padding-left: 20px;
      font-size: 18px;
    }
  }
}
</style>
